package com.wang.test.demo.json2sql;

import com.alibaba.fastjson.JSONObject;
import com.wang.test.demo.entity.Test;
import com.wang.test.demo.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class Json2Sql {

    @Autowired
    private TestMapper testMapper;

    public static void main(String[] args) {
        Json2Sql json2Sql = new Json2Sql();
        json2Sql.jsonToSql();
    }

    public void jsonToSql(){

        //准备要解析的json字符串
        String json = "[\n" +
                "  {\n" +
                "    \"id\": \"155\",\n" +
                "    \"name\": \"小米\",\n" +
                "    \"state\": \"A\",\n" +
                "    \"createTime\": \"2021-08-25\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id\": \"105\",\n" +
                "    \"name\": \"小明\",\n" +
                "    \"state\": \"B\",\n" +
                "    \"createTime\": \"2021-08-25\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id\": \"115\",\n" +
                "    \"name\": \"小红\",\n" +
                "    \"state\": \"B\",\n" +
                "    \"createTime\": \"2021-08-25\"\n" +
                "  }\n" +
                "]";
        //2.将字符串转成list集合
        List<Test> list = JSONObject.parseArray(json, Test.class);

        //3.保存到数据库 ---这里使用mybatis-plus
        //list.forEach(x -> testMapper.insert(x));
        //4.我们直接打印集合看看映射对不对
        list.forEach(x -> System.out.println(x));
    }
}
